<template>
	<view class="wrap">
		<view class="top-data" v-if="state.mine">
			<view class="b-main">
				<view class="b-amount">
					<view class="iconfont icon-line"></view>
					<view class="s-num">{{state.mine.amount_left}}</view>
					<view class="iconfont icon-line"></view>
				</view>
				<view class="s-tit">剩余礼币</view>
			</view>
		</view>
		<view class="record-list">
			<view class="b-item" v-for="(item, index) in state.items" :key="index">
				<view class="b-main">
					<view class="s-title">{{item.action_text}}</view>
					<view class="s-amount">{{item.amount}}</view>
				</view>
				<view class="b-info">
					<view class="s-time">{{timeFormat(item.created_at)}}</view>
					<view class="s-left">剩余 {{item.after_amount}}</view>
				</view>
			</view>
		</view>
		<!-- 加载中 -->
		<load-more :loadingType="loadingType" :loadingText="loadingText"></load-more>
	</view>
</template>

<script>
	import {
		getMineFund,
		getFundRecord,
	} from '@/service/api/user.js'
	import mixinsCommon from '@/mixins/common.js'
	import mixinsAuth from '../../mixins/auth.js'
	import loadMore from '@/components/load-more/load-more.vue'
	export default {
		mixins: [mixinsCommon, mixinsAuth],
		data() {
			return {
				state: {
					mine: null,
					items: []
				},
				group: 'all',
				loadingType: 1,
				loadingText: '',
			}
		},
		onLoad(options) {
			this.loadMineFund()
			this.loadFundRecord(true)
		},
		onReachBottom() {
			if (this.loadingType !== 1 && this.loadingType !== 2) {
				this.loadFundRecord();
			}
		},
		methods: {
			loadMineFund() {
				getMineFund().then(([err, res]) => {
					console.log('getMineFund', err, res)
					if (!err) {
						this.state.mine = res
					}
				})
			},
			loadFundRecord(refresh) {
				console.log('loadFundRecord', refresh);
				if (refresh) {
					this.page = 1;
					this.state.items = []
				} else {
					this.page++
				}
				this.loadingType = 1
				this.loadingText = ''
				getFundRecord(this.page, 10).then(([err, res]) => {
					console.log('getFundRecord', err, res)
					this.loadingType = -1;
					if (!err) {
						if (res.items.length > 0) {
							this.state.items = [...this.state.items, ...res.items]
						} else {
							this.loadingType = 2
							if (this.page == 1) {
								this.loadingText = '暂无数据'
							}
							this.page-- // 重置分页
						}
					} else {
						this.loadingType = 3
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	.wrap {}

	.top-data {
		background: #FFC300;
		height: 260upx;
		display: flex;
		align-items: center;
		justify-content: center;

		.b-main {
			display: flex;
			flex-direction: column;
			align-items: center;
			color: #fff;

			.b-amount {
				display: flex;
				align-items: center;

				.iconfont {
					font-size: 28upx;
				}

				.s-num {
					margin: 0 20upx;
					font-size: 60upx;
				}
			}

			.s-tit {
				margin-top: 10upx;
				font-size: 28upx;
			}
		}
	}

	.record-list {
		background: #fff;

		.b-item {
			padding: 30upx;
			border-bottom: 1upx solid #eee;

			&:last-child {
				border-bottom: none;
			}

			.b-main {
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: #333;

				.s-title {
					font-size: 28upx;
				}

				.s-amount {
					font-size: 32upx;
				}
			}

			.b-info {
				margin-top: 20upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: #999;
				font-size: 24upx;

				.s-time {}

				.s-left {}
			}
		}
	}
</style>
